package com.augurit.common.system.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import com.augurit.common.constant.OrgConstant;
import com.augurit.common.syscode.web.form.SysCodeForm;
import com.augurit.common.sysconfig.service.ISysConfigService;
import com.augurit.common.sysconfig.web.form.SysConfigForm;
import com.augurit.common.system.service.org.IAcApplicationService;
import com.augurit.common.system.service.org.IAcRoleService;
import com.augurit.common.system.service.org.IAcServerService;
import com.augurit.common.system.service.org.IAtHomepageConfigService;
import com.augurit.common.system.web.org.form.AcApplicationForm;
import com.augurit.common.system.web.org.form.AcRoleForm;
import com.augurit.common.system.web.org.form.AcServerForm;
import com.augurit.common.system.web.org.form.AtHomepageConfigForm;

public class WebContextUtils {
	/**
	 * 系统帮助文档存放地址
	 */
	public static final String HELP_DOC_PATH ="help/奥格采集应用平台操作说明书v1.0.doc";
	
	/**
	 * 后台系统管理是否已经初始化成功
	 */
	public static final String FRAMEWORK_SYSTEM_INIT = "FRAMEWORK_SYSTEM_INIT";
	
	/**
	 * 个性化配置信息
	 */
	public static final String SYS_CONFIG_IS_CLUSTER = "SYS_CONFIG_IS_CLUSTER";
	public static final String SYS_CONFIG_CSS = "SYS_CONFIG_CSS";
	public static final String SYS_CONFIG_HOMEPAGE = "SYS_CONFIG_HOMEPAGE";
	public static final String SYS_CONFIG_PAGE_SIZE = "SYS_CONFIG_PAGE_SIZE";
	
	/**
	 * 存放系统首页配置信息的application变量名
	 */
	public static final String APP_HOMEPAGE_CONFIG = "APP_HOMEPAGE_CONFIG";
	
	/**
	 * 缓存数据字典
	 */
	public static final String APP_SYS_CODE = "APP_SYS_CODE";
	
	/**
	 * 存放WEB服务器列表信息的application变量名
	 */
	public static final String APP_SERVER_LIST = "APP_SERVER_LIST";
	
	/**
	 * 存放WEB服务器列表信息的application变量名
	 */
	public static final String APP_APPLICATION_MAP = "APP_APPLICATION_MAP";
	
	/**
	 * 存放登录用户信息的SESSION变量名
	 */
	public static final String SES_LOGIN_USER = "SES_LOGIN_USER";
	
	/**
	 * 注意：应该去掉该变量，以后整改。陈卫光
	 */
	public static final String SES_LOGIN_USER_FUNC = "SES_LOGIN_USER_FUNC";
	
	/**
	 * 存放登录错误的SESSION变量名
	 */
	public static final String SES_LOGIN_ERROR = "SES_LOGIN_ERROR";
	
	/**
	 * 存放预加载的菜单数据的request变量名
	 */
	public static final String REQ_PRELOAD_MENU_STORE = "PRELOAD_MENU_STORE";
	
	/**
	 * 存放用户自定义快捷菜单的request变量名
	 */
	public static final String REQ_USER_SHORTCUT_STORE = "REQ_USER_SHORTCUT_STORE";


	/**
	 * 存放服务器部署路径的会话变量
	 */
	public static String SERVER_REAL_PATH = "SERVER_REAL_PATH";
	/**
	 * 存放系统版本的request变量名
	 */
	public static final String SYS_VERSION	="SYS_VERSION";
	
	
	/**
	 * 检查后台系统管理是否已经初始化
	 * 如果后台系统管理已经初始化，则返回true，否则返回false
	 */
	public static boolean checkSystemInit(ISysConfigService sysConfigService, ServletContext servletContext){
		String homepageCode = sysConfigService.getSystemHomepageCode();

		if(homepageCode != null && homepageCode.trim().length() > 0){
			servletContext.setAttribute(FRAMEWORK_SYSTEM_INIT, OrgConstant.ONE);
			return true;
		}else{
			servletContext.setAttribute(FRAMEWORK_SYSTEM_INIT, OrgConstant.ZERO);
			return false;
		}
	}
	
	/**
	 * 如果后台系统管理已经初始化，则返回true，否则返回false
	 * @param servletContext
	 * @return
	 */
	public static boolean isSystemInit(ServletContext servletContext){
		String initFlag = (String)servletContext.getAttribute(FRAMEWORK_SYSTEM_INIT);
		if(initFlag.equals(OrgConstant.ONE))
			return true;
		else
			return false;
	}
	
	/**
	 * 是否启用集群
	 * @param servletContext
	 * @return
	 */
	public static boolean isCluster(ServletContext servletContext){
		String isCluster = (String)servletContext.getAttribute(SYS_CONFIG_IS_CLUSTER);
		if(isCluster.equals(OrgConstant.ONE))
			return true;
		else
			return false;
	}
	
	/**
	 * 获取数据字典缓存对象
	 * 注意：如果是集群环境则直接返回NULL
	 * @return
	 */
	public static Map<String, List<SysCodeForm>> getSysCodeBuffer(ServletContext servletContext){
		if(!WebContextUtils.isCluster(servletContext))
			return (Map<String, List<SysCodeForm>>)servletContext.getAttribute(APP_SYS_CODE);
		else
			return null;
	}
	
	/**
	 * 建立空的数据字典缓存
	 * @param servletContext
	 */
	public static void buildSysCodeBuffer(ServletContext servletContext){
		if(!WebContextUtils.isCluster(servletContext)){
			servletContext.setAttribute(WebContextUtils.APP_SYS_CODE, new HashMap<String, List<SysCodeForm>>());
		}
	}
	

	/**
	 * 删除数据字典缓存中的指定数据字典项
	 * @param typeIds 要删除的类型项ID集合
	 * @param sysCodeService
	 * @param servletContext
	 */
	public static void syncSysCodeBuffer(List<String> typeCodeList, ServletContext servletContext){
		Map<String, List<SysCodeForm>> sysCodeMap = WebContextUtils.getSysCodeBuffer(servletContext);
		if(sysCodeMap != null){
			if(typeCodeList != null && typeCodeList.size() > 0)
				for(String typeCode : typeCodeList){
					sysCodeMap.remove(typeCode);
			}
		}
	}
	
	/**
	 * 同步更新个性化配置信息
	 * @param sysConfigService
	 * @param servletContext
	 * @return
	 */
	public static boolean syncSysConfig(ISysConfigService sysConfigService, ServletContext servletContext){
		List<SysConfigForm> list = sysConfigService.getAll();
		
		if(list != null && list.size() > 0){
			for(SysConfigForm form : list){
				servletContext.setAttribute(form.getKey(), form.getValue());
			}
			return true;
		}
		return false;
	}
	
	/**
	 * 同步更新系统首页配置信息的application变量名
	 * @param atHomepageConfigService
	 * @param servletContext
	 * @return true表示存在可用的系统首页配置信息，false表示不存在
	 */
	public static void syncAtHomepageConfig(IAtHomepageConfigService atHomepageConfigService, ServletContext servletContext){
		String homeCode = (String)servletContext.getAttribute(SYS_CONFIG_HOMEPAGE);

		if(homeCode != null && homeCode.trim().length() > 0){
			AtHomepageConfigForm form = atHomepageConfigService.getActiveHompageConfigByCode(homeCode);
			servletContext.setAttribute(APP_HOMEPAGE_CONFIG, OrgUtils.getValidHomepageConfig(form));
		}
	}
	
	/**
	 * 获取系统首页配置缓存对象
	 * @param servletContext
	 * @return
	 */
	public static AtHomepageConfigForm getAtHomepageConfigBuffer(ServletContext servletContext){
		return (AtHomepageConfigForm)servletContext.getAttribute(APP_HOMEPAGE_CONFIG);
	}
	
	/**
	 * 同步更新WEB服务器列表的application变量名（应用缓存或集群环境都适用）
	 * @param acServerService
	 * @param servletContext
	 * @return
	 */
	public static boolean syncAcServer(IAcServerService acServerService, ServletContext servletContext){
		return syncAcServer(acServerService, null, servletContext);
	}
	
	/**
	 * 同步更新WEB服务器列表的application变量名（应用缓存或集群环境都适用）
	 * @param acServerService
	 * @param servletContext
	 * @return
	 */
	public static boolean syncAcServer(IAcServerService acServerService, IAcApplicationService acApplicationService, ServletContext servletContext){
		if(!WebContextUtils.isCluster(servletContext)){
			List<AcServerForm> acServerList = acServerService.getAll();
			
			if(acServerList != null && acServerList.size() > 0){
				servletContext.setAttribute(APP_SERVER_LIST, acServerList);
				
				//同步应用列表
				if(acApplicationService != null)
					syncAcApplication(acApplicationService, servletContext);
				
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 获取Web服务器列表（应用缓存或集群环境都适用）
	 * @param servletContext
	 * @return
	 */
	public static List<AcServerForm> getAcServerList(IAcServerService acServerService, ServletContext servletContext){
		if(isCluster(servletContext))
			return acServerService.getAll();
		else
			return (List<AcServerForm>)servletContext.getAttribute(APP_SERVER_LIST);
	}
	
	/**
	 * 同步更新应用列表的application变量名（应用缓存或集群环境都适用）
	 * @param acApplicationService
	 * @param acServerList
	 * @param servletContext
	 * @return
	 */
	public static boolean syncAcApplication(IAcApplicationService acApplicationService, ServletContext servletContext){
		if(!WebContextUtils.isCluster(servletContext)){
			List<AcApplicationForm> acApplicationList = acApplicationService.getAllApps();
			
			if(acApplicationList != null && acApplicationList.size() > 0){
				
				//获取Web服务器列表
				List<AcServerForm> acServerList = (List<AcServerForm>)servletContext.getAttribute(APP_SERVER_LIST);
				
				Map<Long, AcApplicationForm> acApplicationMap = OrgUtils.converToAppMap(acApplicationList, acServerList);
				servletContext.setAttribute(APP_APPLICATION_MAP, acApplicationMap);
				
				return true;
			}
		}	
		return false;
	}
	
	/**
	 * 获取应用MAP（应用缓存或集群环境都适用）
	 * @param servletContext
	 * @return
	 */
	public static Map<Long, AcApplicationForm> getAcApplicationMap(IAcServerService acServerService, 
			IAcApplicationService acApplicationService, ServletContext servletContext){
		
		if(isCluster(servletContext)){
			List<AcApplicationForm> acApplicationList = acApplicationService.getAllApps();
			List<AcServerForm> acServerList = acServerService.getAll();
			return OrgUtils.converToAppMap(acApplicationList, acServerList);
		}
		else
			return (Map<Long, AcApplicationForm>)servletContext.getAttribute(APP_APPLICATION_MAP);
	}
	
	/**
	 * 获取应用列表（应用缓存或集群环境都适用）
	 * @param onlyLoadActiveApp 是否只加载启用的应用列表
	 * @param servletContext
	 * @return
	 */
	public static List<AcApplicationForm> getAcApplicationList(IAcServerService acServerService, 
			IAcApplicationService acApplicationService, boolean onlyLoadActiveApp, ServletContext servletContext){
		
		Map<Long, AcApplicationForm> map = getAcApplicationMap(acServerService, acApplicationService, servletContext);
		return OrgUtils.convertToAcApplicationList(map, onlyLoadActiveApp);
	}
}
